package com.briup.jd2407cms.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.briup.jd2407cms.converter.CategoryParentIdConverter;
import com.briup.jd2407cms.domain.Category;
import com.briup.jd2407cms.domain.extend.CategoryExtend;
import com.briup.jd2407cms.result.Result;
import com.briup.jd2407cms.service.CategoryService;
import com.briup.jd2407cms.utils.ExcelUtil;
import com.briup.jd2407cms.web.listener.CategoryListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "栏目管理")
@RestController
@RequestMapping("/auth/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    ExcelUtil excelUtil;

    @ApiOperation("查询所有父栏目")
    @GetMapping("/queryAllOneLevel")
    public Result<List<Category>> findAllParent(){
        return Result.success(categoryService.getAllParent());
    }

    @ApiOperation("新增栏目")
    @PostMapping("/save")
    public Result<String> addCategory(@RequestBody Category category){
        categoryService.saveCategory(category);
        return Result.success("新增栏目成功");
    }

    @ApiOperation("根据id查询栏目信息")
    @GetMapping("/queryById/{id}")
    public Result<Category> findCategoryById(@PathVariable Integer id){
        return Result.success(categoryService.getByCategoryId(id));
    }

    @ApiOperation("更新栏目信息")
    @PutMapping("/update")
    public Result<String> editCategoryInfo(@RequestBody Category category){
        categoryService.modifyCategory(category);
        return Result.success("更新栏目信息成功");
    }


    @ApiOperation("删除逻辑，批量和单个放在一起")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result removeCategoryByIds(@PathVariable List<Integer> ids){
        if(ids.size() == 1){
            categoryService.removeCategoryById(ids.get(0));
        }else {

        }
        return Result.success("批量删除成功");
    }

    @ApiOperation("根据parentId条件分页查询所有栏目")
    @GetMapping("/query")
    public Result<IPage<Category>> findAllByConditionByPage(@RequestParam Integer pageNum,
                                                     @RequestParam Integer pageSize,
                                                     Integer parentId){
        return Result.success(categoryService.getByConditionByPage(pageNum, pageSize, parentId));
    }

    @ApiOperation("查询所有一级栏目及联下属的二级栏目")
    @GetMapping("/queryAllParent")
    public Result<List<CategoryExtend>> findAllParentWithChild(){
        return Result.success(categoryService.getAllWithChildren());
    }

    @ApiOperation("上传excel栏目数据")
    @PostMapping("/import")
    public Result<?> importExcel(@RequestPart MultipartFile file){
        // 从数据库中获取所有的一级栏目集合，更新一下，防止提前被更新过
        CategoryParentIdConverter.list = categoryService.getAllParent();
        // 获取文件内导入的数据
        List<Category> categories = excelUtil.importData(file, Category.class, new CategoryListener());
        categoryService.insertBatch(categories);

        return Result.success("excel导入栏目数据成功");
    }

    // 下载文件不需要返回统一的结果响应
    // 需要自己包装response
    @ApiOperation("下载excel文件")
    @GetMapping(value = "/export",produces = "application/octet-stream")
    public void exportExcel(HttpServletResponse response){
        // 1、获取所有的父级栏目，保证数据库里是最新的
        CategoryParentIdConverter.list = categoryService.getAllParent();
        // 2、查询所有栏目的数据
        List<Category> categoryList = categoryService.getAll();
        // 3、提供下载
        excelUtil.exportExcel(response,categoryList,Category.class,"栏目信息表");
    }

}
